package com.example.recycler.okhtttp;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView mTvCenter;
    private String url="http://192.168.1.44:8080/xutils/ser";
    private Handler mHandler=new Handler();
    public void go1(View v) {
        Toast.makeText(this, "Get方式", Toast.LENGTH_SHORT).show();
        OkHttpClient okHttpClient=new OkHttpClient();
        Map<String,String> map=new HashMap<>();
        map.put("aaa","直死魔眼两仪式");
        map.put("bbb","邪龙魔女黑贞德");
        Headers headers=Headers.of(map);
        Request.Builder builder=new Request.Builder()
                .url(url)
                .headers(headers)
                .method("GET",null);
        final Request request=builder.build();
        System.out.println(request+"");
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                System.out.println(response.body().string());
                    mTvCenter.setText(response.body().string());
            }
        });

    }

    public void go2(View v) {
        Toast.makeText(this, "POST网络访问", Toast.LENGTH_SHORT).show();
        OkHttpClient okHttpClient=new OkHttpClient();
        RequestBody requestBody=new FormBody.Builder()
                .add("username","风华绝世_两仪式")
                .add("userpwd","倾国倾城_黑贞德")
                .build();
        Headers headers=Headers.of("aaa","Saber","bbb","Avengers");
        Request.Builder builder=new Request.Builder()
                .url(url)
                .headers(headers)
                .method("POST",requestBody);
        final Request request=builder.build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                    final String dataa=response.body().string();
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mTvCenter.setText(dataa);
                        }
                    });
            }
        });
    }

    public void go3(View v) {


    }

    public void go4(View v) {


    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvCenter = (TextView) findViewById(R.id.tv_center);
    }

}
